package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.hc.client5.http.fluent.Request;

import Biz.inputTestedBeanLocal;

/**
 * Servlet implementation class addTestedServlet
 */
@WebServlet("/addTestedServlet")
public class addTestedServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addTestedServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();

		String ID=(String) session.getAttribute("ID");
		String name=(String) session.getAttribute("name");
		String ID_P=request.getParameter("ID");

		
		if(name!=null&&ID!=null)
		{
			inputTestedBeanLocal _inputTestedBean=(inputTestedBeanLocal)session.getAttribute("_inputTestedBean");
			_inputTestedBean.addStudent(ID);
			session.removeAttribute("ID");
			session.removeAttribute("name");
		}
		else if(ID_P!=null)
		{
			String URI = "http://localhost:8888/StudentService/studentService?ID=" + ID_P;


			name  = Request.get(URI).execute().returnContent().toString();

			if(!name.isEmpty())
			{
				session.setAttribute("ID", ID_P);
				session.setAttribute("name", new String(name.getBytes("iso-8859-1")));
			}

			
			request.getRequestDispatcher("/NameConfirm.jsp").forward(request, response);
			return;
		}
		request.getRequestDispatcher("/InputTestedName.jsp").forward(request, response);


	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
